package com.sophialex.health.record.common.draw;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Hyperlink;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;

/**
 * 界面绘制类
 * <p>
 * Title:Health
 * </p>
 * <p>
 * DesignUI
 * </p>
 * Alex May 22, 2011 10:03:33 AM create
 * 
 * @author <a href='wanghao924@126.com'>Alex</a> QQ: 873987060;
 * @version 1.0
 */
public class DesignUI {
	
	public static int oneDefaultTop = 40;
	
	public static int defaultWidth = 166;
	
	public static int wightmargin = 2;

	/**
	 * 绘制 ScrolledForm
	 * 
	 * @param toolkit
	 * @param container
	 * @return
	 * @author Alex May 22, 2011 10:11:37 AM create
	 */
	public static ScrolledForm drawScrolledForm(FormToolkit toolkit, Composite container) {
		ScrolledForm scrolledForm = toolkit.createScrolledForm(container);
		toolkit.paintBordersFor(scrolledForm);
		scrolledForm.getBody().setLayout(new FormLayout());
		return scrolledForm;
	}

	/**
	 * 绘制 Section
	 * 
	 * @return
	 * @author Alex May 22, 2011 10:11:29 AM create
	 */
	public static Section drawSection(FormToolkit toolkit, ScrolledForm scrlledForm, UIFormAttachment uiFormAttachement, String title, boolean twistie) {
		int style = Section.DESCRIPTION | Section.TITLE_BAR;
		if(twistie) {
			style = Section.TWISTIE | Section.TITLE_BAR;
		}
		Section section = toolkit.createSection(scrlledForm.getBody(), style);
		FormData fd_section = new FormData();
		fd_section.top = uiFormAttachement.getTop();
//		fd_section.bottom = uiFormAttachement.getBottom();
		fd_section.left = uiFormAttachement.getLeft();
		fd_section.right = uiFormAttachement.getRight();
		section.setLayoutData(fd_section);
		toolkit.paintBordersFor(section);
		section.setText(title);
		if(twistie) {
			section.setExpanded(true);
		}
		return section;
	}

	/**
	 * 绘制 Section 默认可以展开
	 * 
	 * @return
	 * @author Alex May 22, 2011 10:11:29 AM create
	 */
	public static Section drawSection(FormToolkit toolkit, ScrolledForm scrlledForm, UIFormAttachment uiFormAttachement, String title) {
		return drawSection(toolkit, scrlledForm, uiFormAttachement, title, true);
	}

	/**
	 * 绘制Section 中的Composite
	 * 
	 * @param toolkit
	 * @param section
	 * @return
	 * @author Alex May 22, 2011 11:13:35 AM create
	 */
	public static Composite drawSectionComposite(FormToolkit toolkit, Section section) {
		Composite composite = toolkit.createComposite(section, SWT.NONE);
		toolkit.paintBordersFor(composite);
		section.setClient(composite);
		composite.setLayout(new FormLayout());
		return composite;
	}
	
	/**
	 * 
	 * 绘制一条线
	 * @param toolkit
	 * @param reference
	 * @author Alex 
	 * May 24, 2011 3:45:14 PM create
	 */
	public static Label drawLine(FormToolkit toolkit,Composite composite,Control reference,int height){
		Label horLine =  new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
		FormData fd_horLine = new FormData();
		if(reference !=null){
			fd_horLine.top = new FormAttachment(reference,height,SWT.TOP);
		}
		fd_horLine.top = new FormAttachment(reference,height,SWT.TOP);
		fd_horLine.left = new FormAttachment(2, 0);
		fd_horLine.right = new FormAttachment(100,-10);
		horLine.setLayoutData(fd_horLine);
		toolkit.adapt(horLine, true, true);
		return horLine;
	}
	
	/**
	 * 
	 * 绘制一条线
	 * @param toolkit
	 * @param reference
	 * @author Alex 
	 * May 24, 2011 3:45:14 PM create
	 */
	public static Label drawLine(FormToolkit toolkit,Composite composite,Control reference){
		return drawLine(toolkit,composite,reference,10);
	}

	/**
	 * 画一个元素（包括label 和 输入控件）
	 * 
	 * @param composite
	 * @param controls
	 * @param isOne
	 *        是否是第一个元素
	 * @param isRow
	 *        是否在换行
	 * @param reference
	 * @author Alex May 22, 2011 11:44:23 AM create
	 */
	public static void drawItem(Composite composite, Control[] controls, boolean isOne, boolean isRow, Control reference) {
		Label lab = (Label) controls[0];
		FormData fd_lab_sh = new FormData();
		if(isOne) {
			if(isRow){
				fd_lab_sh.top = new FormAttachment(reference, oneDefaultTop,SWT.TOP);
				fd_lab_sh.left = new FormAttachment(0);
			}else{
				fd_lab_sh.top = new FormAttachment(0, 20);
				fd_lab_sh.left = new FormAttachment(0);
			}
		} else {
			if(isRow) { 
				fd_lab_sh.top = new FormAttachment(reference, 40, SWT.TOP);
				fd_lab_sh.right = new FormAttachment(reference, 0, SWT.RIGHT);
			} else {
				fd_lab_sh.left = new FormAttachment(reference, 40);
				fd_lab_sh.bottom = new FormAttachment(reference, 0, SWT.BOTTOM);
			}

		}
		lab.setLayoutData(fd_lab_sh);
		Control control = controls[1];
		FormData fd_txtNewText = new FormData();
		fd_txtNewText.right = new FormAttachment(lab, defaultWidth, SWT.RIGHT);
		fd_txtNewText.top = new FormAttachment(lab, -8, SWT.TOP);
		fd_txtNewText.bottom = new FormAttachment(lab, 0, SWT.BOTTOM);
		fd_txtNewText.left = new FormAttachment(lab, 6);
		control.setLayoutData(fd_txtNewText);
		if(controls.length==3){
			Control pix = controls[2];
			if(pix instanceof Label || pix instanceof Hyperlink){
				FormData fd_pix = new FormData();
				fd_pix.bottom = new FormAttachment(control,0, SWT.BOTTOM);
				fd_pix.left = new FormAttachment(control,3);
				pix.setLayoutData(fd_pix);
			}else{
				FormData fd_pix = new FormData();
				fd_pix.left = new FormAttachment(control,wightmargin);
				fd_pix.right = new FormAttachment(control,defaultWidth+wightmargin,SWT.RIGHT);
				fd_pix.top = new FormAttachment(lab, -8, SWT.TOP);
				fd_pix.bottom = new FormAttachment(lab, 0, SWT.BOTTOM);
				pix.setLayoutData(fd_pix);
			}
			
		}
	}
	
	/**
	 * 画一个元素（包括label 和 输入控件）
	 * 
	 * @param composite
	 * @param controls
	 * @param isOne
	 *        是否是第一个元素
	 * @param isRow
	 *        是否在换行
	 * @param reference
	 * @author Alex May 22, 2011 11:44:23 AM create
	 */
	public static void drawItem(Composite composite, Control[] controls, boolean isOne, boolean isRow, Control reference,int width) {
		Label lab = (Label) controls[0];
		FormData fd_lab_sh = new FormData();
		if(isOne) {

			if(isRow){
				fd_lab_sh.top = new FormAttachment(reference, 40,SWT.TOP);
				fd_lab_sh.left = new FormAttachment(0);
			}else{
				fd_lab_sh.top = new FormAttachment(0, 20);
				fd_lab_sh.left = new FormAttachment(0);
			}
		} else {
			if(isRow) { 
				fd_lab_sh.top = new FormAttachment(reference, 40, SWT.TOP);
				fd_lab_sh.right = new FormAttachment(reference, 0, SWT.RIGHT);
			} else {
				fd_lab_sh.left = new FormAttachment(reference, 40);
				fd_lab_sh.bottom = new FormAttachment(reference, 0, SWT.BOTTOM);
			}

		}
		lab.setLayoutData(fd_lab_sh);
		Control control = controls[1];
		FormData fd_txtNewText = new FormData();
		fd_txtNewText.right = new FormAttachment(lab, width, SWT.RIGHT);
		fd_txtNewText.top = new FormAttachment(lab, -8, SWT.TOP);
		fd_txtNewText.bottom = new FormAttachment(lab, 0, SWT.BOTTOM);
		fd_txtNewText.left = new FormAttachment(lab, 6);
		control.setLayoutData(fd_txtNewText);
		
		if(controls.length==3){
			Control pix = controls[2];
			FormData fd_pix = new FormData();
			fd_pix.bottom = new FormAttachment(control,0, SWT.BOTTOM);
			fd_pix.left = new FormAttachment(control,3);
			pix.setLayoutData(fd_pix);
		}
		if(controls.length==4){
			Control pix = controls[2];
			FormData fd_pix = new FormData();
			fd_pix.bottom = new FormAttachment(control,0, SWT.BOTTOM);
			fd_pix.left = new FormAttachment(control,10);
			pix.setLayoutData(fd_pix);
			Control pix1 = controls[3];
			FormData fd_pix1 = new FormData();
			fd_pix1.left = new FormAttachment(controls[2],10);
			fd_pix1.right = new FormAttachment(controls[2],width,SWT.RIGHT);
			fd_pix1.top = new FormAttachment(lab, -8, SWT.TOP);
			fd_pix1.bottom = new FormAttachment(lab, 0, SWT.BOTTOM);
			pix1.setLayoutData(fd_pix1);
		}
		if(controls.length==6){
			Control pix = controls[2];
			FormData fd_pix = new FormData();
			fd_pix.bottom = new FormAttachment(control,0, SWT.BOTTOM);
			fd_pix.left = new FormAttachment(control,10);
			pix.setLayoutData(fd_pix);
			
			Control pix1 = controls[3];
			FormData fd_pix1 = new FormData();
			fd_pix1.left = new FormAttachment(controls[2],20);
			fd_pix1.right = new FormAttachment(controls[2],width,SWT.RIGHT);
			fd_pix1.top = new FormAttachment(lab, -8, SWT.TOP);
			fd_pix1.bottom = new FormAttachment(lab, 0, SWT.BOTTOM);
			pix1.setLayoutData(fd_pix1);
			
			Control pix2 = controls[4];
			FormData fd_pix2 = new FormData();
			fd_pix2.left = new FormAttachment(controls[3],20);
			fd_pix2.bottom = new FormAttachment(lab, 0, SWT.BOTTOM);
			pix2.setLayoutData(fd_pix2);
			
			Control pix3 = controls[5];
			FormData fd_pix3 = new FormData();
			fd_pix3.left = new FormAttachment(controls[4],20);
			fd_pix3.right = new FormAttachment(controls[4],width,SWT.RIGHT);
			fd_pix3.top = new FormAttachment(lab, -8, SWT.TOP);
			fd_pix3.bottom = new FormAttachment(lab, 0, SWT.BOTTOM);
			pix3.setLayoutData(fd_pix3);
		}
	}
	
    /**
     * 追加元素
     * @param target  目标元素
     * @param orig    要追加的元素
     * @param width   与目标元素的宽度
     * @author Alex 
     * May 24, 2011 11:20:51 AM create
     */
	public static void drawAddTo(Control target,Control orig,int width){
		FormData fd_orig = new FormData();
		fd_orig.left = new FormAttachment(target,width);
		if(orig instanceof Label){
			
		}else{
//			fd_orig.right = new FormAttachment(target,width,SWT.RIGHT);
//			fd_orig.top = new FormAttachment(target, -8, SWT.TOP);
		}
		
		fd_orig.bottom = new FormAttachment(target, 0, SWT.BOTTOM);
		orig.setLayoutData(fd_orig);
	}
	
	  /**
     * 追加元素
     * @param target  目标元素
     * @param orig    要追加的元素
     * @param width   与目标元素的宽度
     * @author Alex 
     * May 24, 2011 11:20:51 AM create
     */
	public static void drawAddTo(Control target,Control orig,int margin,Control reference,int width){
		FormData fd_orig = new FormData();
		fd_orig.left = new FormAttachment(target,margin);
		if(orig instanceof Label){
			
		}else{
			fd_orig.right = new FormAttachment(target,width,SWT.RIGHT);
			if(reference instanceof Label){
				fd_orig.top = new FormAttachment(reference,-8, SWT.TOP);
			}else{
				fd_orig.top = new FormAttachment(reference,0, SWT.TOP);
			}
			
		}
		
		fd_orig.bottom = new FormAttachment(target, 0, SWT.BOTTOM);
		orig.setLayoutData(fd_orig);
	}
	
	
	/**
	 * 画一个元素（包括label 和 输入控件）
	 * 
	 * @param composite
	 * @param controls
	 * @param isOne
	 *        是否是第一个元素
	 * @param isRow
	 *        是否在换行
	 * @param reference
	 * @author Alex May 22, 2011 11:44:23 AM create
	 */
	public static void drawItem(Composite composite, Control[] controls, boolean isOne, boolean isRow, Control reference,Control top) {
		Label lab = (Label) controls[0];
		FormData fd_lab_sh = new FormData();
		if(isOne) {

			if(isRow){
				fd_lab_sh.top = new FormAttachment(reference, 40,SWT.TOP);
				fd_lab_sh.left = new FormAttachment(0);
			}else{
				fd_lab_sh.top = new FormAttachment(0, 20);
				fd_lab_sh.left = new FormAttachment(0);
			}
		} else {
			if(isRow) { 
				fd_lab_sh.top = new FormAttachment(reference, 40, SWT.TOP);
				fd_lab_sh.right = new FormAttachment(reference, 0, SWT.RIGHT);
			} else {
				fd_lab_sh.left = new FormAttachment(reference, 40);
				fd_lab_sh.bottom = new FormAttachment(reference, 0, SWT.BOTTOM);
			}

		}
		lab.setLayoutData(fd_lab_sh);
		Control control = controls[1];
		FormData fd_txtNewText = new FormData();
		fd_txtNewText.right = new FormAttachment(top, 0, SWT.RIGHT);
		fd_txtNewText.top = new FormAttachment(lab, -8, SWT.TOP);
		fd_txtNewText.bottom = new FormAttachment(lab, 0, SWT.BOTTOM);
		fd_txtNewText.left = new FormAttachment(lab, 6);
		control.setLayoutData(fd_txtNewText);

	}
	
    
    /**
     * 绘制一行元素
     * @param composite
     * @param controlList
     * @param reference
     * @param isOne  是否是第一行
     * @author Alex 
     * May 22, 2011 12:18:34 PM create
     */
	public static void drawRow(Composite composite, List<Control[]> controlList, Control reference,boolean isOne) {
        Control refer=null;
		for(int i = 0; i < controlList.size(); i++) {
			Control[] control = controlList.get(i);
			if(i == 0) {
				if(isOne){
					drawItem(composite, control, true, false, reference);
				}else{
					drawItem(composite, control, false, true, reference);
				}
			} else {
                drawItem(composite, control, false, false, refer);
			}
            if(control.length>2){
            	refer = control[control.length-1];
            }else{
            	refer = control[1];
            }
			
		}
	}
    
	  /**
     * 绘制一行元素
     * @param composite
     * @param controlList
     * @param reference
     * @param isOne  是否是第一行
     * @author Alex 
     * May 22, 2011 12:18:34 PM create
     */
	public static void drawRow(Composite composite, List<Control[]> controlList, Control reference,boolean isOne,boolean isRow) {
        Control refer=null;
		for(int i = 0; i < controlList.size(); i++) {
			Control[] control = controlList.get(i);
			if(i == 0) {
				if(isRow){
					if(isOne){
						drawItem(composite, control, true, true, reference);
					}else{
						drawItem(composite, control, false, true, reference);
					}
				}
				
			} else {
                drawItem(composite, control, false, false, refer);
			}
            if(control.length>2){
            	refer = control[control.length-1];
            }else{
            	refer = control[1];
            }
		}
	}
    
    
	
	 /**
     * 绘制一行元素
     * @param composite
     * @param controlList
     * @param reference
     * @param isOne  是否是第一行
     * @author Alex 
     * May 22, 2011 12:18:34 PM create
     */
	public static void drawRow(Composite composite, List<Control[]> controlList, Control[] reference,boolean isOne) {
		for(int i = 0; i < controlList.size(); i++) {
			Control[] control = controlList.get(i);
			if(i == 0) {
				if(isOne){
					drawItem(composite, control, true, false, reference[i]);
				}else{
					drawItem(composite, control, false, true, reference[i]);
				}
			} else {
                drawItem(composite, control, false, true, reference[i]);
			}
		 
		}
	}
	
	
    /**
     * 绘制一个带内容的区域
     * @param toolkit
     * @param scrlledForm
     * @param uiFormAttachement
     * @param title
     * @param controlList
     * @param column   一行的元素个数
     * @param refertop   是否相对上面的元素
     * @author Alex 
     * May 22, 2011 12:48:07 PM create
     */
	public static void drawSecionContent(Composite composite,List<Control[]> controlList,int column,boolean refertop,Control reference){
		int total = controlList.size();
		int rowCount = getTotalRow(total,column);
		Control refer=null;
		Control[] refers = null;
		int to;
		for(int i = 0; i < rowCount; i++) {
			to = i*column+column;
			if(to>total){
				to = total;
			}
			List<Control[]> rowControlList = controlList.subList(i*column,to);
			if(i==0){
				if(reference!=null){
					DesignUI.drawRow(composite, rowControlList, reference, true,true);
				}else{
					DesignUI.drawRow(composite, rowControlList, refer, true);
				}
				
			}else{
				if(refertop){
					DesignUI.drawRow(composite, rowControlList, refers, false);
				}
				else{
					DesignUI.drawRow(composite, rowControlList, refer, false);
				}
			}
			refer = rowControlList.get(0)[0];
			if(refertop){
				refers = new Control[rowControlList.size()];
				for(int j = 0; j < rowControlList.size(); j++) {
					Control[] temp = rowControlList.get(j);
					refers[j] = temp[0];
				}
			}
		
			
		}
	}
	


	
	   /**
     * 绘制一个带内容的区域
     * @param toolkit
     * @param scrlledForm
     * @param uiFormAttachement
     * @param title
     * @param controlList
     * @param column   一行的元素个数
     * @param refertop   是否相对上面的元素
     * @author Alex 
     * May 22, 2011 12:48:07 PM create
     */
	public static void drawSecionContent(Composite composite,List<Control[]> controlList,int column){
		drawSecionContent(composite,controlList,column,false,null);
	}
	
	/**
	 * 获得总行数
	 * @param totalControls
	 * @param columnNum
	 * @return
	 * @author Alex 
	 * May 22, 2011 12:55:11 PM create
	 */
	private static int getTotalRow(int totalControls,int columnNum){
		if (totalControls <= 0)
			return -1;
		int temp = totalControls % columnNum;
		int tempp = totalControls / columnNum;
		return (temp == 0?tempp:tempp + 1);
	}
	
	public static void reset(){
		oneDefaultTop = 40;
		defaultWidth = 166;
		wightmargin = 2;
	}
}
